<!--pages/traffic/paySuccess.wxml-->
<template>
  <view>
    <view class="text-p1">支付完成!</view>
    <view class="text-p1" v-if="!isRecharge">请在20分钟内完成出场</view>
    <view class="counpon" v-if="coupon">
      <image src="../../image/counpon.png" />
      <view class="counpon-content">
        <view class="p1">
          临时停车
          <text>{{coupon.deduction}}元</text>优惠券
        </view>
        <view class="p2">满{{coupon.useStartPrice}}元可用</view>
        <view
            class="p2"
            v-if="coupon.validType==1"
          >{{coupon.validStartDay.substring(0,10)}}~{{coupon.validEndDay.substring(0,10)}}</view>
      </view>
      <view class="counpon-btn" @tap="receivecoupon(coupon.id)" v-if="!isReceive">立即领取</view>
      <view class="counpon-btn" style="background:#EFEFEF;color:#909090" v-else>已领取</view>
    </view>
    <view class="box">
      <image :src="imgUrl+'/success1.jpg'" class="box-img"  @tap="goHome"/>
    </view>
    <view v-if="isFirstLoad">
      <view class="panel-black"></view>
      <view class="box1">
       <image :src="imgUrl+'/payImg.png'" class="box1-img1"/>
       <image :src="imgUrl+'/know.png'" class="box1-img2" @tap="closedAdd()"/>
    </view>
    </view>
    <view v-if="!hasPhone">
      <view class="panel-black"></view>
      <view class="panel-box">
        <view class="panel-box-t1">提示</view>
        <view class="panel-box-t2">尚未获取手机号，无法领取优惠券</view>
        <button
        openType="getPhoneNumber"
        @GetPhoneNumber="onGetPhoneNumber"
        class="panel-box-btn"
          style="height:100rpx;"
      >获取手机号</button>
      </view>
    </view>
    <!-- <view class="btn" @tap="goBack">确定</view> -->
  </view>
</template>
<script>
import Taro from "@tarojs/taro";
import {setGlobalData,getGlobalData} from "../../globalData.js";
import authorize from "../../authorize.js";
import "./paySuccess.less";
import bulterApi from '@souche2014/bulter-api'
const api = bulterApi({ partner: 'IOParking' })
export default {
  data() {
    return {
      isRecharge: false,
      hasPhone:true,
      mobile: "",
      appId:"",
      coupon: null,
      apiBaseUrl: process.env.IOParking.API_HOST, //请求头
      imgUrl: process.env.IOParking.IMG_URL,
      parkId: 0,
      isReceive:false,//是否领取到优惠券
      couponParkId:'',
      isFirstLoad:false,
    };
  },
  mixins:[authorize],
  onLoad: function(options) {
    this.isRecharge = options.isRecharge == "1" ? true : false;
    this.parkId = options.parkId;
    this.getData();
    // if(this.parkId==5){
    //   this.isFirstLoad=true
    // }
  },
  methods: {
    closedAdd(){
      this.isFirstLoad=false
    },
    goHome(){
       //api.openPage("home");
       api.bury("PARK_COMPLETE_ENTER", {});
       Taro.redirectTo({
        url: "../carSale/home"
      });
    },
    goBack() {
      Taro.redirectTo({
        url: "../home/home"
      });
    },
    getData() {
      var that = this;
      var base = that.apiBaseUrl;
      that.setDataloading = true;
      Taro.showLoading({
        title: "加载中",
        mask: true
      });
      Taro.request({
        url: base + "/carcoupon/alllist",
        data: {
          pubPlace: 2,//1：缴费页，2：支付完成页
          parkId: that.parkId
        },
        header: {
          "content-type": "application/json" // 默认值
        },
        method: "POST",
        success(res) {
          Taro.hideLoading();
          console.log(res.data.data);
          if (res.data.isSuccess) {
            that.coupon = res.data.data;
            if(res.data.data){
              that.couponParkId = res.data.data.parkId;
            }
          }
        }
      });
    },
    receivecoupon(couponId) {
      var that = this;
      if (getGlobalData("mobile") == "") {
        that.hasPhone = false;
        return
      }
      var base = that.apiBaseUrl;
      Taro.request({
        url: base + "/carcoupon/receivecoupon",
        data: {
          mobile: getGlobalData("mobile")||"",
          wxOpenId: getGlobalData("openId"),
          couponId: couponId,
          parkId: that.couponParkId
        },
        header: {
          "content-type": "application/json" // 默认值
        },
        method: "POST",
        success(res) {
          console.log(res.data.data);
          if (res.data.isSuccess) {
            Taro.showToast({
              title: "领取成功",
              icon: "success",
              duration: 600,
              mask: true
            });
            that.isReceive=true
          } else {
            Taro.showToast({
              title: res.data.message,
              icon: "error",
              duration: 800,
              mask: true
            });
          }
        }
      });
    },
    handleCoupon(item) {
      var flag = 1;
      var date = new Date();
      let year = date.getFullYear(); //获取完整的年份(4位)
      let month = date.getMonth() + 1; //获取当前月份
      month = month < 10 ? "0" + month : month + "";
      let day = date.getDate(); //获取当前日(1-31)
      day = day < 10 ? "0" + day : day + "";
      let time = year + "/" + month + "/" + day;
      let useStartTime = time + " " + item.useStartTime + ":00";
      let useEndTime = time + " " + item.useEndTime + ":00";
      console.log(useStartTime, useEndTime);
      useStartTime = new Date(useStartTime.replace(/-/g, "/")).valueOf();
      useEndTime = new Date(useEndTime.replace(/-/g, "/")).valueOf();
      let nowTime = new Date().valueOf();

      if (nowTime < useStartTime || nowTime > useEndTime) {
        console.log("不可领取");
        Taro.showToast({
          title: "该时段不可领取",
          icon: "error",
          duration: 1000,
          mask: true
        });
        flag = 0;
      }
      return flag;
    },
    
  }
};
</script>

